<template>
  <div class="community-display">
    <!-- Search and Add section -->
    <div class="search-container">
      <div class="search-input-group">
        <el-input v-model="searchQuery" placeholder="请输入关键字" class="search-input">
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
        <el-button @click="clearSearch" icon="el-icon-delete" class="clear-btn">清空</el-button>
      </div>
      
      <el-button type="primary" @click="handleAdd" icon="el-icon-plus" class="add-btn">添加</el-button>
    </div>

    <!-- Data table -->
    <div class="community-table-container">
      <div class="table-header">
        <span>楼宇列表</span>
      </div>
      
      <el-table :data="buildingList" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="80" align="center" fixed></el-table-column>
        <el-table-column prop="code" label="楼宇编号" width="120"></el-table-column>
        <el-table-column prop="name" label="楼宇名称" width="150" align="center"></el-table-column>
        <el-table-column prop="layers" label="楼宇总层数" width="105"></el-table-column>
        <el-table-column prop="structure" label="建筑结构" width="225"></el-table-column>
        <el-table-column prop="remark" label="备注" width="145"></el-table-column>
        <el-table-column label="操作" width="240">
          <template slot-scope="scope">
            <div class="action-buttons">
              <el-button type="primary" size="mini" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
              <el-button type="success" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- Pagination -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      currentPage: 1,
      pageSize: 10,
      total: 10,
      buildingList: []
    }
  },

  methods: {
    handleSearch() {
      // 实现搜索逻辑
      console.log('Searching for:', this.searchQuery);
      // 这里应该调用API获取数据
    },

    clearSearch() {
      this.searchQuery = '';
      this.fetchData();
    },

    handleAdd() {
      // 跳转到添加页面或打开添加对话框
      console.log('Add new community');
    },

    handleView(row) {
      console.log('View community:', row);
    },

    handleEdit(row) {
      console.log('Edit community:', row);
    },

    handleDelete(row) {
      console.log('Delete community:', row);
      this.$confirm('此操作将永久删除该社区, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除API
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },

    fetchData() {
        // 调用后端API获取数据
         this.$axios.get('api/infrastructure-server/building/queryAll')
        .then(response => {
          if (response.data && response.data.code === 200) {
            this.buildingList = response.data.data;
          }
        })
        .catch(error => {
          console.error('获取社区列表错误:', error);
        });
    }
  },

  created() {
    this.fetchData();
  }
}
</script>

<style scoped>
.community-display {
  padding: 20px;
}

.search-container {
  background-color: #fff;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  border-radius: 4px;
}

.search-input-group {
  display: flex;
  align-items: center;
}

.search-input {
  width: 300px;
  margin-right: 10px;
}

.clear-btn {
  margin-left: 10px;
}

.community-table-container {
  background-color: #fff;
  border-radius: 4px;
  padding-bottom: 20px;
}

.table-header {
  padding: 15px;
  border-bottom: 1px solid #ebeef5;
  font-weight: bold;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.action-buttons {
  display: flex;
  justify-content: space-between;
}

.action-buttons .el-button {
  padding: 4px 8px;
  margin: 0 3px;
}
</style>